<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id ="app">
        <h2>人员列表</h2>
        <input type="text" placeholder="请输入名字" v-model="keyWord">
        <ul>
            <li v-for="person in show_persons" :key="person.id">
                {{person.name}}-{{person.age}}-{{person.sex}}
            </li>
        </ul>
    </div>

    <script>
        //创建Vue实例,得到 ViewModel
        const vm = new Vue({
            el: '#app',
            // 自定义属性
            data: {
                keyWord:'',
                persons:[
                    { id:'001', name:'马冬梅', age:18, sex:'女' },
                    { id:'002', name:'周冬雨', age:19, sex:'女' },
                    { id:'003', name:'周杰伦', age:20, sex:'男' },
                    { id:'004', name:'温兆伦', age:21, sex:'男' }
                ],
                show_persons:[]
            },
            // 自定义计算属性
            computed: {

            },
            // 自定义方法
            methods: {},
            watch: {
                keyWord: {
                    immediate: true,
                    handler(val) {
                        console.log(val)
                        // filter(callback)  callback返回true表示保留，返回false表示剔除
                        this.show_persons = this.persons.filter((p)=>{
                            return p.name.indexOf(val) !== -1
                        })
                    }
                }
            }
        });
    </script>
</body>
</html>